package com.zhouyu.service;

import com.zhouyu.mapper.OrderMapper;
import com.zhouyu.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.annotation.Retention;
import java.util.Date;

//@Component
//@Service
//@Conditional(value = ZhouyuCondition.class)
//@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//@Lazy
@Component
public class UserService /*implements UserServiceInterface*/ /*extends BaseService<OrderService, StockService>*/ /*implements BeanNameAware*/ /*implements UserInterface<OrderService>*/ /*implements SmartInitializingSingleton*/ /*implements ApplicationContextAware*/ {

	//@Autowired
	//private UserMapper userMapper;
	//
	//
	//@Autowired
	//private OrderMapper orderMapper;
	//
	//@Autowired
	//private SqlSession sqlSession;

	//@Autowired
	//private JdbcTemplate jdbcTemplate;

	//@Autowired
	//private UserService userService;

	//查看数据库连接情况: show status like '%Threads_connected%';

	//同一个数据库连接要么同时提交，要么同时回滚，不会存在同时提交和回滚的情况

	@Transactional
	public void test() {

		//TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
		//	@Override
		//	public void suspend() {
		//		System.out.println("111");
		//	}
		//
		//	@Override
		//	public void resume() {
		//
		//	}
		//
		//	@Override
		//	public void beforeCommit(boolean readOnly) {
		//
		//	}
		//
		//	@Override
		//	public void beforeCompletion() {
		//
		//	}
		//
		//	@Override
		//	public void afterCommit() {
		//
		//	}
		//
		//	@Override
		//	public void afterCompletion(int status) {
		//
		//	}
		//});

		//System.out.println(TransactionSynchronizationManager.getCurrentTransactionName());

		//jdbcTemplate.execute("insert into t1 values(1, 1, 1, 1, '1')");
		//userService.a();

		try {
			throw new RuntimeException();
		} catch (Exception e) {

			//强制回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//返回友好的提示给客户端
			return;
		}
	}


	//@Transactional(propagation = Propagation.REQUIRES_NEW)
	////不支持Spring事务
	//@Transactional(propagation = Propagation.NOT_SUPPORTED)
	////底层创建一个savepoint
	//@Transactional(propagation = Propagation.NESTED)
	@Transactional
	public void a() {
		//jdbcTemplate.execute("insert into t1 values(2, 2, 2, 2, '2')");
		//throw new NullPointerException();
		//System.out.println(TransactionSynchronizationManager.getCurrentTransactionName());
	}

	//@Autowired
	//private OrderService orderService;
	//
	//public void test() {
	//
	//	//System.out.println(userMapper.selectById());
	//	//System.out.println(orderMapper.selectById());
	//
	//
	//	//sqlSession.selectOne("com.zhouyu.mapper.UserMapper.selectById");
	//	//sqlSession.selectOne("com.zhouyu.mapper.UserMapper.selectById");
	//
	//	System.out.println("test");
	//}
	//
	//public void a() {
	//	System.out.println("a");
	//}
	//
	//public void b(String a, String b) {
	//	System.out.println(orderService);
	//
	//}


	//private String beanName;

	//@Autowired
	//private UserService userService;


	//@Autowired
	//@Random
	//private LoadBalance orderService;

	//到IOC容器中寻找beanName为zhouyu的bean对象
	//@Value(value = "#{zhouyu}")
	//private OrderService orderService;


	//@Resource
	//private OrderService orderService;


	//@Autowired
	//private OrderService orderService;

	//public UserService() {
	//	System.out.println(0);
	//}


	//@Autowired(required = false)
	//public UserService(OrderService orderService) {
	//	this.orderService = orderService;
	//	System.out.println(1);
	//}

	//@Autowired(required = false)
	//public UserService(OrderService orderService, OrderService orderService1) {
	//	this.orderService = orderService;
	//	System.out.println(2);
	//}


	//@Autowired(required = false)
	//public UserService(OrderService orderService, OrderInterface orderService1, OrderService orderService2) {
	//	this.orderService = orderService;
	//	System.out.println(3);
	//}
	//
	//@Autowired(required = false)
	//public UserService(OrderService orderService, OrderService orderService1, OrderInterface orderService2) {
	//	this.orderService = orderService;
	//	System.out.println(4);
	//}

	//private String orderService;

	//@Override
	//@Autowired
	//public void setOrderService(OrderService orderService) {
	//
	//}

	//@Autowired
	//public void setOrderService(String orderService) {
	//	this.orderService = orderService;
	//}


	//public OrderService getOrderService() {
	//	return orderService;
	//}
	//

	///**
	// * autowire="byType" 找到set方法，根据方法参数的类型去IOC容器中找bean对象,找到了，然后传给这个set方法，调用这个set方法
	// *
	// * autowire="byName" 找到set方法，根据setXxxx，截取set后面的字符，第一个字符小写，
	// * 		例如：xxxx，将xxxx当作beanName去IOC容器中找bean对象，找到了就将bean对象传给这个set方法，调用这个set方法
	// *
	// * @param orderService
	// */
	//public void setOrderService(OrderService orderService) {
	//	System.out.println("123");
	//	this.orderService = orderService;
	//}

	//
	//public UserService() {
	//	System.out.println(0);
	//}
	//
	//public UserService(OrderService orderService) {
	//	System.out.println(1);
	//	this.orderService = orderService;
	//}

	//@Override
	//public void afterSingletonsInstantiated() {
	//
	//}

	//class Member {}

	//class Person {}
	//
	//@Value(value = "zhouyu")
	//private User user;

	//private ApplicationContext applicationContext;

	//@Autowired
	//private ApplicationEventPublisher applicationEventPublisher;

	//@Autowired
	//private OrderService orderService;
	//
	//
	//@Autowired
	//private JdbcTemplate jdbcTemplate;
	//
	//@Autowired
	//private UserService userService;

	//public void a() {
	//	System.out.println("xxxxxx");
	//}

	//@Lookup(value = "orderService")
	//public OrderService a() {
	//	return null;
	//}

	//@EventListener
	//public void a(ApplicationEvent applicationEvent) {
	//
	//	System.out.println(applicationEvent);
	//
	//}

	//@Transactional
	//public void test(){
	//
	//	//applicationContext.publishEvent("123123");
	//
	//	//applicationEventPublisher.publishEvent("123123");
	//
	//	//System.out.println("test----->" + orderService);
	//	//System.out.println(userService);
	//
	//
	//	//System.out.println(orderService);
	//
	//	//jdbcTemplate.execute("insert t1 values(1, 1, 1, 1, '1')");
	//
	//	//userService.a();
	//
	//	//System.out.println(orderService);
	//}

	//@Override
	//public void setBeanName(String name) {
	//	this.beanName = name;
	//}
	//
	//@Override
	//public String toString() {
	//	return "UserService{" +
	//			"beanName='" + beanName + '\'' +
	//			'}';
	//}


	//@Override
	//public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	//	this.applicationContext = applicationContext;
	//}

	//@Transactional(propagation = Propagation.NEVER)
	//public void a(){
	//
	//}

	@Async
	@Transactional
	public void async() {
		System.out.println("async");
	}

}
